#include "threadpool.h"

int login(int* netfd, MYSQL* db,char* userName, char* userPasswd){
    train_t train;
    memset(&train, 0, sizeof(train_t));
    int ret = recv(*netfd,&train.command,sizeof(train.command),0);
    if(ret == 0){
        close(*netfd);
        *netfd = -1;
        return -1;
    }
    recv(*netfd, &train.length,sizeof(train.length),0);
    recv(*netfd,train.data,train.length,0);
    uint8_t userNameLen = train.data[0];
    memcpy(userName,train.data + 1,userNameLen);
    uint8_t userPasswdLen = train.data[userNameLen + 1];
    memcpy(userPasswd, train.data + userNameLen + 2, userPasswdLen);
    switch(train.command){
    case REGISTER:{
                      char salts[21];
                      salt(salts);
                      struct crypt_data data;
                      memset(&data,0,sizeof(data));
                      char* key = crypt_r(userPasswd,salts,&data);
                      if(key == NULL){
                          return -1;
                      }
                      char sql[1024] = {0};
                      sprintf(sql,"insert into user (username,salt,passwd,is_deleted) values('%s','%s','%s','1');",userName,salts,key);
                      int qret = mysql_query(db,sql);
                      if(qret != 0){
                          printf("error: %s\n",mysql_error(db));
                          train.command = REGISTER_FAIL;
                          memcpy(train.data,"register failed",sizeof(train.data));
                          train.length = strlen(train.data);
                          send(*netfd,&train,sizeof(train.command) + sizeof(train.length) + train.length, 0);
                          return -1;
                      }
                      train.command = REGISTER_SUCCESS;
                      memcpy(train.data,"register success",sizeof(train.data));
                      train.length = strlen(train.data);
                      send(*netfd,&train,sizeof(train.command) + sizeof(train.length) + train.length, 0);
                      break;
                  }
    case LOGIN:{
                   char sql[1024] ={0};
                   sprintf(sql,"select salt,passwd from user where username = '%s' and is_deleted = 1",userName);
                   int qret = mysql_query(db,sql);
                   if(qret != 0){
                       printf("error: %s\n",mysql_error(db));
                       train.command = LOGIN_FAIL;
                       memcpy(train.data,"login failed",sizeof(train.data));
                       train.length = strlen(train.data);
                       send(*netfd,&train,sizeof(train.command) + sizeof(train.length) + train.length, 0);
                       return -1;
                   }
                   MYSQL_RES* userMes =  mysql_store_result(db);
                   MYSQL_ROW row = mysql_fetch_row(userMes);
                   if(row == NULL){
                       train.command = LOGIN_FAIL;
                       memcpy(train.data,"user not find or is deleted",sizeof(train.data));
                       train.length = strlen(train.data);
                       send(*netfd,&train,sizeof(train.command) + sizeof(train.length) + train.length, 0);
                       return -1;
                   }
                   char* salt = row[0];
                   char* passwd = row[1];
                   struct crypt_data data;
                   char* key = crypt_r(userPasswd,salt,&data);
                   int ret = strcmp(key,passwd);
                   if(ret != 0){
                       train.command = LOGIN_FAIL;
                       memcpy(train.data,"user passwd error",sizeof(train.data));
                       train.length = strlen(train.data);
                       send(*netfd,&train,sizeof(train.command) + sizeof(train.length) + train.length, 0);
                       return -1;
                   }
                   train.command = LOGIN_SUCCESS;
                   memcpy(train.data,"login success",sizeof(train.data));
                   train.length = strlen(train.data);
                   send(*netfd,&train,sizeof(train.command) + sizeof(train.length) + train.length, 0);
                   return 0;
               }
    default: break;
    }

}

int salt(char* salts){
    char arr[] = {
        'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z',
        'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z',
        '0','1','2','3','4','5','6','7','8','9','.','/'};
    srand(time(NULL));
    char salt[21] ={0};
    salt[0] = '$';
    salt[1] = '6';
    salt[2] = '$';
    salt[19] = '$';
    for(int i = 3; i < 19; i++){
        salt[i] = arr[rand() % strlen(arr)];
    }
    salt[20] = '\0';
    memcpy(salts,salt,sizeof(salt));
    return 0;
} 

